package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.domain.SkuPropertyValue;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.SkuPropertyMapper;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.vo.GenerateSkuVO;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class SkuPropertyServiceImpl implements ISkuPropertyService {
    @Autowired
    private SkuPropertyMapper skuPropertyMapper;
    @Autowired
    private CatalogMapper catalogMapper;

    /**
     * 查询对应商品的sku属性列表
     * @param catalogId
     * @return
     */
    @Override
    public List<SkuProperty> selectByCatalogId(Long catalogId) {
        return skuPropertyMapper.selectByCatalogId(catalogId);
    }

    /**
     * 查询sku属性
     * @param skuPropertyId
     * @return
     */
    @Override
    public SkuProperty selectById(Long skuPropertyId) {
        return skuPropertyMapper.selectByPrimaryKey(skuPropertyId);
    }

    /**
     * 生成sku 数据S
     * @param generateSkuVO
     * @return
     */
    @Override
    public List<Map<String, Object>> generateSku(GenerateSkuVO generateSkuVO) {

        //sku属性值的列表
        List<SkuPropertyValue> skuPropertyValueList = generateSkuVO.getSkuPropertyValueList();

        //商品
        Product product = generateSkuVO.getProduct();

        //1.把数据源转成 Map<Long, List<SkuPropertyValue>> 结构,进行分组
        Map<Long, List<SkuPropertyValue>> groupMap = new HashMap<>() ;

        for(SkuPropertyValue skuPropertyValue : skuPropertyValueList){
            Long key = skuPropertyValue.getSkuPropertyId();

            List<SkuPropertyValue> skuPropertyValues = groupMap.get(key);
            if(skuPropertyValues == null){
                skuPropertyValues = new ArrayList<>();
                groupMap.put(key , skuPropertyValues);
            }
            skuPropertyValues.add(skuPropertyValue) ;
        }
        System.out.println(groupMap);

        //2.把 Map<Long, List<SkuPropertyValue>>  转换成 List<List<SkuPropertyValue>>  使用map.values();
        List<List<SkuPropertyValue>> groupList = new ArrayList<>(groupMap.values()) ;
        System.out.println(groupList);
        List<List<SkuPropertyValue>> resultList = new ArrayList<>() ;


        //3.把 List<List<SkuPropertyValue>> 转成 List<List<SkuPropertyValue>>
        generateSkuRecursive(groupList , resultList , 0 , new ArrayList<SkuPropertyValue>());

        //4.把笛卡尔积的二维数组 变成 List<Map<String,Object>>
        List<Map<String,Object>> resultMap = new ArrayList<>();

        int  number = 1 ;
        for(List<SkuPropertyValue> skuPropertyValues : resultList){
            // {price:100,code:110 , 7:黑色, 8:移动},
            Map<String,Object> map = new HashMap<>();
            map.put("price",product.getBasePrice());
            map.put("code",getSkuCode(product,number));  //getSkuCode(product,number)
            for (SkuPropertyValue skuPropertyValue : skuPropertyValues){
                map.put(skuPropertyValue.getSkuPropertyId().toString(), skuPropertyValue.getValue());
            }
            resultMap.add(map);
            number = number + 1;
        }
        return resultMap;
    }
    public static void generateSkuRecursive( List<List<SkuPropertyValue>> source , List<List<SkuPropertyValue>> result
            , int number ,List<SkuPropertyValue> tempList){

        List<SkuPropertyValue>  skuPropertyValues = source.get(number);

        //如果是最后一层
        if(number < source.size() - 1){
            //非最后一层
            for(SkuPropertyValue skuPropertyValue : skuPropertyValues){
                List<SkuPropertyValue> tempSkuPropertyValue = new ArrayList<>(tempList);
                tempSkuPropertyValue.add(skuPropertyValue);
                generateSkuRecursive(source , result ,number+1 ,tempSkuPropertyValue );
            }
        }else{
            //最后一层
            for(SkuPropertyValue skuPropertyValue : skuPropertyValues){
                List<SkuPropertyValue> tempSkuPropertyValue = new ArrayList<>(tempList);
                tempSkuPropertyValue.add(skuPropertyValue);
                //添加到结果
                result.add(tempSkuPropertyValue);
            }
        }
    }
    public String getSkuCode(Product product,int number){

        Catalog catalog = catalogMapper.selectByPrimaryKey(product.getCatalog().getId());
               List<Catalog> catalogs = new ArrayList<>() ;

        while(true){

            catalogs.add(catalog) ;
            Long pId = catalog.getPId();
            if(pId == null){
                break;
            }
            catalog = catalogMapper.selectByPrimaryKey(pId);
        }

        Collections.reverse(catalogs);

        StringBuilder sb = new StringBuilder() ;
        for(int  i = 1 ; i < catalogs.size() ; i++){
            Catalog tempCataLog = catalogs.get(i);
            if(i == 1){
                sb.append(tempCataLog.getCode().substring(0,2).toUpperCase());
            }else if(i == 2 || i == 3){
                sb.append(tempCataLog.getSort() < 10?"0"+tempCataLog.getSort():tempCataLog.getSort());
            }
        }
        sb.append(product.getId() < 10 ?"0"+product.getId():product.getId());
        sb.append(number < 10 ?"0"+number:number);
        return sb.toString();
    }
}
